home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / lds / init.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  343 lines

  1. /*************************************************************************
  2.  *                                                                       *
  3.  *  Copyright (c) 1992, 1993 Ronald Joe Record                           *
  4.  *                                                                       *
  5.  *  All rights reserved. No part of this program or publication may be   *
  6.  *  reproduced, transmitted, transcribed, stored in a retrieval system,  *
  7.  *  or translated into any language or computer language, in any form or *
  8.  *  by any means, electronic, mechanical, magnetic, optical, chemical,   *
  9.  *  biological, or otherwise, without the prior written permission of:   *
  10.  *                                                                       *
  11.  *      Ronald Joe Record (408) 458-3718                                 *
  12.  *      212 Owen St., Santa Cruz, California 95062 USA                   *
  13.  *                                                                       *
  14.  *************************************************************************/
  15.  
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include "x.h"
  23. #include "defines.h"
  24. #include "externals.h"
  25. #include "xexterns.h"
  26.  
  27. XPoint *hpoints;
  28.  
  29. /* routines in this file */
  30. void init_data(), init_canvas(), init_mem(), init_gen();
  31.  
  32. /* external routines */
  33. extern void InitBuffer(), phasediff(), grafline(), grafgen(), zero_mem();
  34. extern void FlushPixmap();
  35.  
  36. void
  37. init_data()
  38. {
  39.     dpy = XOpenDisplay("");
  40.     screen = DefaultScreen(dpy);
  41.     background = BlackPixel(dpy, screen);
  42.     foreground = WhitePixel(dpy, screen);
  43.     srand48(getpid());
  44.     if (width == 0)
  45.       if ((width = wide) == 0)
  46.         if (two_dim)
  47.           width = DEF_2D_WIDTH;
  48.         else
  49.           width = XDisplayWidth(dpy, screen);
  50.     if (height == 0)
  51.       if ((height = high) == 0)
  52.         if (two_dim)
  53.           height = DEF_2D_HEIGHT;
  54.         else
  55.           height = XDisplayHeight(dpy, screen);
  56.     if (wide == 0)
  57.         wide = width;
  58.     if (high == 0)
  59.         if (two_dim)
  60.             high = height;
  61.         else
  62.             high = 1;
  63.     if (high == 1) { /* one dimensional LDS */
  64.         EU = ED = 0.0;
  65.         EC = 1.0 - (ER + EL);
  66.     }
  67.     else /* two dimensional LDS */
  68.         EC = 1.0 - (EU + ED + ER + EL);
  69.     numgen=1;
  70.     parity = numgen % freq;
  71.     incline=omega/lamval;
  72.     decline=omega/(lamval-1.0);
  73.     numcolors = XDisplayCells(dpy, XDefaultScreen(dpy));
  74.     if (numcolors > MAXCOLOR)
  75.         numcolors = MAXCOLOR;
  76.     mincolor = (numcolors/16) + 1;
  77.     displayplanes = DisplayPlanes(dpy, XDefaultScreen(dpy));
  78.     xpoint = ypoint = 0;
  79.     InitBuffer(&Points, numcolors);
  80. }
  81.  
  82. void
  83. init_canvas()
  84. {
  85.     static int i;
  86.  
  87.     /*
  88.      * create default, writable, graphics contexts for the canvas.
  89.      */
  90.     for (i=0; i<numcolors; i++) {
  91.         Data_GC[i] = XCreateGC(dpy, DefaultRootWindow(dpy),
  92.             (unsigned long) NULL, (XGCValues *) NULL);
  93.         /* set the background to black */
  94.         XSetBackground(dpy,Data_GC[i],BlackPixel(dpy,XDefaultScreen(dpy)));
  95.         /* set the foreground of the ith context to i */
  96.         XSetForeground(dpy, Data_GC[i], i);
  97.     }
  98.     XSetForeground(dpy,Data_GC[0],BlackPixel(dpy,XDefaultScreen(dpy)));
  99.     XSetForeground(dpy,Data_GC[1],WhitePixel(dpy,XDefaultScreen(dpy)));
  100. }
  101.  
  102. /*
  103.  *  init_mem() - malloc necessary memory
  104.  */
  105. void
  106. init_mem()
  107. {
  108.     static int i;
  109.  
  110.     if ((diff = (double *)malloc(wide*sizeof(double)))==NULL){
  111.             printf("Error malloc'ing diff. Exiting\n");
  112.             exit(-1);
  113.     }
  114.     if ((histogram=(int *)malloc((unsigned)(wide*sizeof(int))))==NULL){
  115.         printf("Error malloc'ing histogram. Exiting\n");
  116.         exit(-1);
  117.     }
  118.     if ((hpoints=(XPoint *)malloc((unsigned)(wide*sizeof(XPoint))))==NULL){
  119.         printf("Error malloc'ing hpoints. Exiting\n");
  120.         exit(-1);
  121.     }
  122.     if ((histarray=(int **)malloc((unsigned)(width*sizeof(int *)))) == 
  123.       (int **)0) {
  124.         printf("Error malloc'ing histarray. Exiting\n");
  125.         exit(-1);
  126.     }
  127.     if ((lambda = (double **)malloc(high*sizeof(double)))==NULL) {
  128.         printf("Error malloc'ing lambda. Exiting\n");
  129.         exit(-1);
  130.     }
  131.     if ((lftconn = (double **)malloc(high*sizeof(double)))==NULL) {
  132.         printf("Error malloc'ing lftconn. Exiting\n");
  133.         exit(-1);
  134.     }
  135.     if ((rgtconn = (double **)malloc(high*sizeof(double)))==NULL) {
  136.         printf("Error malloc'ing rgtconn. Exiting\n");
  137.         exit(-1);
  138.     }
  139.     if ((ctrconn = (double **)malloc(high*sizeof(double)))==NULL) {
  140.         printf("Error malloc'ing ctrconn. Exiting\n");
  141.         exit(-1);
  142.     }
  143.     if ((uprconn = (double **)malloc(high*sizeof(double)))==NULL) {
  144.         printf("Error malloc'ing uprconn. Exiting\n");
  145.         exit(-1);
  146.     }
  147.     if ((lwrconn = (double **)malloc(high*sizeof(double)))==NULL) {
  148.         printf("Error malloc'ing lwrconn. Exiting\n");
  149.         exit(-1);
  150.     }
  151.     if ((avg=(double **)malloc(high*sizeof(double)))==NULL){
  152.         printf("Error malloc'ing avg. Exiting\n");
  153.         exit(-1);
  154.     }
  155.     if ((currentgen=(double **)malloc(high*sizeof(double)))==NULL){
  156.         printf("Error malloc'ing currentgen. Exiting\n");
  157.         exit(-1);
  158.     }
  159.     if ((nextgen = (double **)malloc(high*sizeof(double)))==NULL) {
  160.         printf("Error malloc'ing nextgen. Exiting\n");
  161.         exit(-1);
  162.     }
  163.     for (i=0; i<width; i++) {
  164.       if ((histarray[i]=(int *)malloc((unsigned)(height*sizeof(int))))==INULL){
  165.         printf("Error malloc'ing histarray[%d]. Exiting\n",i);
  166.         exit(-1);
  167.       }
  168.     }
  169.     for (i=0; i<high; i++) {
  170.       if ((lambda[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  171.         printf("Error malloc'ing lambda. Exiting\n");
  172.         exit(-1);
  173.       }
  174.       if ((lftconn[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  175.         printf("Error malloc'ing lftconn. Exiting\n");
  176.         exit(-1);
  177.       }
  178.       if ((rgtconn[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  179.         printf("Error malloc'ing rgtconn. Exiting\n");
  180.         exit(-1);
  181.       }
  182.       if ((uprconn[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  183.         printf("Error malloc'ing uprconn. Exiting\n");
  184.         exit(-1);
  185.       }
  186.       if ((lwrconn[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  187.         printf("Error malloc'ing lwrconn. Exiting\n");
  188.         exit(-1);
  189.       }
  190.       if ((ctrconn[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  191.         printf("Error malloc'ing ctrconn. Exiting\n");
  192.         exit(-1);
  193.       }
  194.       if ((avg[i] = (double *)malloc(wide*sizeof(double)))==NULL){
  195.         printf("Error malloc'ing avg. Exiting\n");
  196.         exit(-1);
  197.       }
  198.       if ((currentgen[i] = (double *)malloc(wide*sizeof(double)))==NULL){
  199.         printf("Error malloc'ing currentgen. Exiting\n");
  200.         exit(-1);
  201.       }
  202.       if ((nextgen[i] = (double *)malloc(wide*sizeof(double)))==NULL) {
  203.         printf("Error malloc'ing nextgen. Exiting\n");
  204.         exit(-1);
  205.       }
  206.     }
  207. }
  208.  
  209. void
  210. init_gen()
  211. {
  212.     static char *tmpvalptr; 
  213.     static int first, i, j, k, F;
  214.  
  215.     zero_mem();
  216.     for (j=0;j<high;j++) {
  217.       tmpvalptr=valinit;
  218.       if ((*valinit != 'r') && (*valinit != 'p') && (iflag)) {
  219.         if (*valinit == 'l') {
  220.             for (i=0;i<wide;i++)
  221.                 currentgen[j][i] = (double)(i+j)/(wide+high);
  222.         }
  223.         else {
  224.             k = (wide/2) - (strlen(valinit)/2) - 1;
  225.             while(*tmpvalptr != '\0') {
  226.                 first = *tmpvalptr++ - '0';
  227.                 currentgen[k++][high/2] = 
  228.                         (double)(first + drand48()) / (double)numcolors;
  229.             }
  230.         }
  231.       }
  232.       else if (*tmpvalptr == 'p') {
  233.         tmpvalptr++;
  234.         if (*tmpvalptr == '\0')
  235.             F = 1;
  236.         else
  237.             while (*tmpvalptr != '\0')
  238.                   F = (F*10)+(*tmpvalptr++ - '0');
  239.         for (i=0;i<wide;i++) {
  240.            currentgen[j][i] =
  241.             (sin((double)((double)(i+j)/(double)(wide+high))*M_PI*F)+1.0)/2.0;
  242.         }
  243.       }
  244.       else {
  245.         for (i=0;i<wide;i++)
  246.             currentgen[j][i] = drand48();
  247.       }
  248.       if (rancon)                /* randomly assign connection values */
  249.         for (i=0;i<wide;i++) {
  250.             ctrconn[j][i] = drand48();
  251.             rgtconn[j][i] = lftconn[j][i] = uprconn[j][i] = lwrconn[j][i] = 
  252.                     (1.0 - ctrconn[j][i])/4.0;
  253.         }
  254.       else if (lincon)                /* linearly assign connection values */
  255.         for (i=0;i<wide;i++) {
  256.             ctrconn[j][i] = (double)(i+j)/(wide+high);
  257.             rgtconn[j][i] = lftconn[j][i] = uprconn[j][i] = lwrconn[j][i] = 
  258.                     (1.0 - ctrconn[j][i])/4.0;
  259.         }
  260.       else if (percon)            /* periodically assign connection values */
  261.         for (i=0;i<wide;i++) {
  262.             ctrconn[j][i] = 
  263.                 (sin(((double)(i+j)/(double)(wide+high))*M_PI*G)+1.0)/2.0;
  264.             rgtconn[j][i] = lftconn[j][i] = uprconn[j][i] = lwrconn[j][i] = 
  265.                     (1.0 - ctrconn[j][i])/4.0;
  266.         }
  267.       else                    /* all connection strengths equal */
  268.         for (i=0;i<wide;i++) {
  269.             ctrconn[j][i] = EC;
  270.             rgtconn[j][i] = ER;
  271.             lftconn[j][i] = EL;
  272.             uprconn[j][i] = EU;
  273.             lwrconn[j][i] = ED;
  274.         }
  275.       if (ranlam)                /* randomly assign parameter values */
  276.         for (i=0;i<wide;i++) {
  277.             if (tent)
  278.                 lambda[j][i] = drand48();
  279.             else if (circle)
  280.                 lambda[j][i] = drand48()/M_2PI;
  281.             else
  282.                 lambda[j][i] = drand48() + 3.0;
  283.         }
  284.       else if (linlam)                /* linearly assign parameter values */
  285.         for (i=0;i<wide;i++) {
  286.             if (tent)
  287.                 lambda[j][i] = ((double)(i+j)/(wide+high));
  288.             else if (circle)
  289.                 lambda[j][i] = ((double)(i+j)/(wide+high))/M_2PI;
  290.             else
  291.                 lambda[j][i] = 3.0 + ((double)(i+j)/(wide+high));
  292.         }
  293.       else if (perlam)                /* periodically assign parameter values */
  294.         for (i=0;i<wide;i++) {
  295.             if (circle)
  296.              lambda[j][i] = 
  297.               ((sin(((double)(i+j)/(double)(wide+high))*M_PI*A)+1.0)/2.0)/M_2PI;
  298.             else if (tent)
  299.              lambda[j][i] = 
  300.                 ((sin(((double)(i+j)/(double)(wide+high))*M_PI*A)+1.0)/2.0);
  301.             else
  302.              lambda[j][i] = 
  303.                 3.0+((sin(((double)(i+j)/(double)(wide+high))*M_PI*A)+1.0)/2.0);
  304.         }
  305.       else if (xflag) {    /* parameter values equal state of cell to left */
  306.         if (circle)
  307.             lambda[j][0] = currentgen[j][wide-1]/M_2PI;
  308.         else if (tent)
  309.             lambda[j][0] = currentgen[j][wide-1];
  310.         else 
  311.             lambda[j][0] = 3.0 + currentgen[j][wide-1];
  312.         for (i=1;i<wide;i++) {
  313.             if (circle)
  314.                 lambda[j][i] = currentgen[j][i-1]/M_2PI;
  315.             else if (tent)
  316.                 lambda[j][i] = currentgen[j][i-1];
  317.             else 
  318.                 lambda[j][i] = 3.0 + currentgen[j][i-1];
  319.         }
  320.       }
  321.       else                     /* all parameter values equal lamval */
  322.         for (i=0;i<wide;i++) {
  323.             if (circle)
  324.                 lambda[j][i] = lamval/M_2PI;
  325.             else 
  326.                 lambda[j][i] = lamval;
  327.         }
  328.       if (begin == 1) {
  329.         if (pflag)
  330.             phasediff(currentgen[j]);
  331.         else if (cflag)
  332.             grafline(currentgen[j]);
  333.         else
  334.             grafgen(currentgen[j]);
  335.       }
  336.     }
  337.     if (!cflag) {
  338.         FlushPixmap(dpy, pixmap, Data_GC, &Points, mincolor, numcolors);
  339.         XCopyArea(dpy, pixmap, canvas, Data_GC[0], 
  340.                         0, height - high, wide, high, 0, height - high);
  341.     }
  342. }
  343.